<template>
  <div class="fixed-table-actionbar">
    <div class="search-bar">
      <el-form :inline="true" :model="formItem" class="form-filter" size="mini">
        <el-form-item>
          <el-select v-model="formItem.s_status" clearable placeholder="请选择巡检任务状态" size="mini" @change="search">
            <el-option v-for="(item) in status" :key="item.value" :label="item.text" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formItem.s_inspectionRuleCode" clearable placeholder="请选择巡检规则" size="mini" @change="search">
            <el-option v-for="(item) in inspectionRules" :key="item.code" :label="item.name " :value="item.code" @click.native="clickOption(item)" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="timeRangeCreateTimeStart"
            clearable
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            value-format="timestamp"
            size="mini"
            @change="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button plain icon="el-icon-search" @click="search" />
        </el-form-item>
      </el-form>
    </div>
    <div class="action-bar">
      <el-button type="primary" size="mini" icon="el-icon-download" @click="exportExcel"> 导出</el-button>
    </div>
  </div>
</template>

<script>
import { clone } from '@neu/biz-core/utils/utils'
import { list } from '@neu/dcom-ui/api/inspectionRule'
import { exportExcel } from '@neu/dcom-ui/api/inspectionTask'
import { formatDate } from '@neu/biz-core/utils/utils'
// 定义一个查询表单原始项
const formItem = {
  s_inspectionRuleCode: '',
  s_inspectionRuleName: '',
  s_status: '',
  s_startTaskTimeBegin: '',
  s_startTaskTimeEnd: '',
  s_statistics: 'true'
}

export default {
  name: 'SysTaskTableFilter',
  data() {
    return {
      /** 启用状态 */
      status: [
        { value: 'notStarted', text: '待巡检' },
        { value: 'inspecting', text: '巡检中' },
        { value: 'overtime', text: '超时未领取' }, // 超过结束时间未领取
        { value: 'overtimeUnfinished', text: '超时未完成' }, // 已领取，但是超过24小时还未填写结果
        { value: 'overtimeFinished', text: '超时完成' }, // 超过24小时，但是已经完成
        { value: 'finished', text: '已完成' }
      ],
      inspectionRules: [],
      formItem: {
        ...formItem
      },
      /** 存储时间范围 */
      timeRangeCreateTimeStart: [],
      /** 时间选择器 */
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  mounted() {
    list().then(response => {
      this.inspectionRules = response.data
    })
  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      // 清除另一个form的检索数据
      if (this.$parent.$refs.filterForm) {
        this.$parent.$refs.filterForm.reset()
      }
      if (this.timeRangeCreateTimeStart && this.timeRangeCreateTimeStart.length !== 0) {
        this.formItem.s_startTaskTimeBegin = this.timeRangeCreateTimeStart[0]
        this.formItem.s_startTaskTimeEnd = this.timeRangeCreateTimeStart[1]
      } else {
        this.formItem.s_startTaskTimeBegin = ''
        this.formItem.s_startTaskTimeEnd = ''
      }
      this.$store.dispatch('dcom/setInspectionSearchObj', this.formItem)
      this.$parent.filter(this.formItem)
    },
    // 重置按钮点击事件处理
    reset: function() {
      this.formItem = {
        ...formItem
      }
      this.search()
    },
    onChange: function(val) {
      this.$parent.onResize()
    },
    onGlobeSearchInputChange(value) {
      if (value === '') {
        this.search()
      }
    },
    clickOption(item) {
      this.formItem.s_inspectionRuleName = item.name
    },
    clearForm() {
      this.formItem = clone(formItem)
    },
    exportExcel() {
      const searchObj = this.$store.state.dcom.inspectionSearchObj
      if (!searchObj || !searchObj.s_inspectionRuleCode) {
        return this.$message({
          showClose: true,
          message: '导出巡检结果必须选择某一巡检规则',
          type: 'error'
        })
      }

      // 默认导出一天时间的
      // if (this.formItem.s_startTaskTimeBegin === '' || this.formItem.s_startTaskTimeEnd === '') {
      //   const start = new Date()
      //   start.setHours(0)
      //   start.setMinutes(0)
      //   start.setSeconds(0)
      //   start.setMilliseconds(0)
      //   const end = new Date()
      //   end.setHours(23)
      //   end.setMinutes(59)
      //   end.setSeconds(59)
      //   end.setMilliseconds(0)
      //   this.formItem.s_startTaskTimeBegin = start.getTime()
      //   this.formItem.s_startTaskTimeEnd = end.getTime()
      // }
      // const time = formatDate(this.formItem.s_startTaskTimeBegin) + '~' + formatDate(this.formItem.s_startTaskTimeEnd)
      let time = ''
      if (searchObj.s_startTaskTimeBegin !== '' && searchObj.s_startTaskTimeEnd !== '') {
        time = formatDate(searchObj.s_startTaskTimeBegin) + '~' + formatDate(searchObj.s_startTaskTimeEnd)
      }
      const fileName = searchObj.s_inspectionRuleName + '巡检结果-' + time + '.xlsx'
      exportExcel(searchObj).then(res => {
        const content = res
        const blob = new Blob([content])

        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
